package pe.edu.upc.dsd;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.rmi.RemoteException;


public class SendEmailImpl implements SendEmail {

	final String miCorreo = "UbicaTuReu@gmail.com";
    final String miContraseña = "*******";
    final String servidorSMTP = "localhost";
    final String puertoEnvio = "25";
    String mailReceptor = null;
    String asunto = null;
    String cuerpo = null;
    
	@Override
	public boolean enviarCorreoWS(String mailReceptor, String asunto,
			String cuerpo) throws RemoteException {
        

        Properties props = new Properties();
        props.put("mail.smtp.user", miCorreo);
        props.put("mail.smtp.host", servidorSMTP);
        props.put("mail.smtp.port", puertoEnvio);
        //props.put("mail.smtp.starttls.enable", "true");
        //props.put("mail.smtp.auth", "true");
        //props.put("mail.smtp.socketFactory.port", puertoEnvio);
        //props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        //props.put("mail.smtp.socketFactory.fallback", "false");

        @SuppressWarnings("unused")
		SecurityManager security = System.getSecurityManager();

        try {
            Authenticator auth = new autentificadorSMTP();
            Session session = Session.getInstance(props, auth);
            session.setDebug(true);

            MimeMessage msg = new MimeMessage(session);
            msg.setText(cuerpo);
            msg.setSubject(asunto);
            msg.setFrom(new InternetAddress(miCorreo));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
                    mailReceptor));
            Transport.send(msg);
            
        } catch (Exception e) {
        	
        	return false;
            
        }
        
        return true;
	}
	
    private class autentificadorSMTP extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(miCorreo, miContraseña);
        }
    }

}
